# -*- coding: UTF-8 -*-
from django.db import models
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError


class Country(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'country'


class Province(models.Model):
    id = models.AutoField(primary_key=True)
    country = models.ForeignKey(Country)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'province'


class Category(models.Model):
    id = models.AutoField(primary_key=True)
    priority = models.IntegerField(blank=True)
    name = models.CharField(max_length=45)
    name_cn = models.CharField(max_length=45)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'category'


class City(models.Model):
    id = models.AutoField(primary_key=True)
    province = models.ForeignKey(Province)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'city'


class Ownership(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'ownership'


class Industry(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'industry'


class ProblemType(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'problem_type'


class CompanyScale(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'company_scale'

class FlashInfo(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=30)
    content = models.CharField(max_length=50)
    img = models.ImageField(upload_to='photos/%Y%m%d%H%M%S',help_text="720X288")
    sort = models.IntegerField(blank=True)

    def __unicode__(self):
        return self.title

    class Meta:
        db_table = u'flash_info'


class Department(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'department'


class EmployeeScale(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)
    name_cn = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name_cn

    class Meta:
        db_table = u'employee_scale'


class Case(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    author = models.CharField(max_length=200, null=True, blank=True)
    user = models.ForeignKey(User)
    category = models.ForeignKey(Category)
    company = models.CharField(max_length=100, null=True, blank=True)
    company_scale = models.ForeignKey(CompanyScale)
    employee_scale = models.ForeignKey(EmployeeScale)
    country = models.IntegerField(null=True, blank=True)
    province = models.IntegerField(null=True, blank=True)
    city = models.IntegerField(null=True, blank=True)
    ownership = models.ForeignKey(Ownership)
    industry = models.ForeignKey(Industry)
    department = models.ForeignKey(Department)
    brief = models.CharField(max_length=500)
    description = models.TextField()
    keywords = models.CharField(max_length=100)
    img = models.ImageField(upload_to='photos/%Y%m%d%H%M%S', null=True, blank=True)
    date = models.DateTimeField(auto_now_add=True)
    count = models.IntegerField(default=0,null=True, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = u'casestudy'


class CaseForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(CaseForm, self).__init__(*args, **kwargs)
        self.fields['department'].choices = tuple(tuple([dm.id, dm.name_cn]) for dm in Department.objects.all())
        self.fields['company_scale'].choices = tuple(tuple([cs.id, cs.name_cn]) for cs in CompanyScale.objects.all())
        self.fields['employee_scale'].choices = tuple(tuple([es.id, es.name_cn]) for es in EmployeeScale.objects.all())
        self.fields['ownership'].choices = tuple(tuple([os.id, os.name_cn]) for os in Ownership.objects.all())
        self.fields['industry'].choices = tuple(tuple([ins.id, ins.name_cn]) for ins in Industry.objects.all())
        self.fields['category'].choices = tuple(tuple([ca.id, ca.name_cn]) for ca in Category.objects.all())

    description = forms.CharField(widget=forms.Textarea(attrs={'class': 'gray'}))
    brief = forms.CharField(widget=forms.Textarea())
    company_scale = forms.ModelChoiceField(label='', widget=forms.Select,
                                           queryset=CompanyScale.objects.all(), required=True)
    employee_scale = forms.ModelChoiceField(widget=forms.Select,
                                            queryset=EmployeeScale.objects.all(),
                                            required=True)
    ownership = forms.ModelChoiceField(widget=forms.Select, queryset=Ownership.objects.all(), required=True)
    industry = forms.ModelChoiceField(widget=forms.Select, queryset=Industry.objects.all(), required=True)
    department = forms.ModelChoiceField(widget=forms.Select, queryset=Department.objects.all(), required=True)
    try:
        country = forms.ChoiceField(required=False,
                                    widget=forms.Select(
                                        attrs={'onchange': 'getProvinceSelectList(this.value,"zh-cn")'}),
                                    choices=(tuple(tuple([c.id, c.name_cn]) for c in Country.objects.all())))
        province = forms.ChoiceField(required=False,
                                     widget=forms.Select(attrs={'onchange': 'getCitySelectList(this.value,"zh-cn")'}),
                                     choices=(tuple(tuple([p.id, p.name_cn])) for p in Province.objects.all()))
        city = forms.ChoiceField(required=False, widget=forms.Select,
                                 choices=(tuple(tuple([ci.id, ci.name_cn])) for ci in City.objects.all()))
    except:
        pass

    class Meta:
        model = Case
        exclude = ('id', 'date', 'user')


class CaseForm_En(ModelForm):
    def __init__(self, *args, **kwargs):
        super(CaseForm_En, self).__init__(*args, **kwargs)
        self.fields['department'].choices = tuple(tuple([dm.id, dm.name]) for dm in Department.objects.all())
        self.fields['company_scale'].choices = tuple(tuple([cs.id, cs.name]) for cs in CompanyScale.objects.all())
        self.fields['employee_scale'].choices = tuple(tuple([es.id, es.name]) for es in EmployeeScale.objects.all())
        self.fields['ownership'].choices = tuple(tuple([os.id, os.name]) for os in Ownership.objects.all())
        self.fields['industry'].choices = tuple(tuple([ins.id, ins.name]) for ins in Industry.objects.all())
        self.fields['category'].choices = tuple(tuple([ca.id, ca.name]) for ca in Category.objects.all())

    description = forms.CharField(widget=forms.Textarea(attrs={'class': 'gray'}))
    brief = forms.CharField(widget=forms.Textarea())
    company_scale = forms.ModelChoiceField(label='', widget=forms.Select,
                                           queryset=CompanyScale.objects.all(), required=True)
    employee_scale = forms.ModelChoiceField(widget=forms.Select, queryset=EmployeeScale.objects.all(), required=True)
    ownership = forms.ModelChoiceField(widget=forms.Select, queryset=Ownership.objects.all(), required=True)
    industry = forms.ModelChoiceField(widget=forms.Select, queryset=Industry.objects.all(), required=True)
    department = forms.ModelChoiceField(widget=forms.Select, queryset=Department.objects.all(), required=True)
    category = forms.ModelChoiceField(widget=forms.Select, queryset=Category.objects.all(), required=True)
    try:
        country = forms.ChoiceField(required=False,
                                    widget=forms.Select(attrs={'onchange': 'getProvinceSelectList(this.value,"en")'}),
                                    choices=(tuple(tuple([c.id, c.name]) for c in Country.objects.all())))
        province = forms.ChoiceField(required=False,
                                     widget=forms.Select(attrs={'onchange': 'getCitySelectList(this.value,"en")'}),
                                     choices=(tuple(tuple([p.id, p.name])) for p in Province.objects.all()))
        city = forms.ChoiceField(required=False, widget=forms.Select,
                                 choices=(tuple(tuple([ci.id, ci.name])) for ci in City.objects.all()))
    except:
        pass

    class Meta:
        model = Case
        exclude = ('id', 'date', 'user')


class Problem(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    name = models.CharField(max_length=200)
    brief = models.CharField(max_length=200)
    description = models.TextField()
    problem_type = models.ForeignKey(ProblemType)
    date = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = u'problem'


class ProblemForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProblemForm, self).__init__(*args, **kwargs)
        self.fields['problem_type'].choices = tuple(
            tuple([problem.id, problem.name]) for problem in ProblemType.objects.all())

    problem_type = forms.ModelChoiceField(label='problem_type', widget=forms.Select,
                                          queryset=ProblemType.objects.all())
    brief = forms.CharField(widget=forms.Textarea())
    description = forms.CharField(widget=forms.Textarea())

    class Meta:
        model = Problem
        exclude = ('id', 'case', 'date')


class Solution(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    problem = models.ForeignKey(Problem)
    name = models.CharField(max_length=200)
    brief = models.CharField(max_length=200)
    description = models.TextField()
    date = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = u'solution'


class SolutionForm(ModelForm):
    brief = forms.CharField(widget=forms.Textarea())
    description = forms.CharField(widget=forms.Textarea())

    class Meta:
        model = Solution
        exclude = ('id', 'case', 'problem', 'date')


class Video(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    name = models.CharField(max_length=200)
    type = models.CharField(max_length=10)
    filesize = models.CharField(max_length=20)
    img = models.ImageField(upload_to='videoPhoto/%Y/%m/%d')
    link = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = u'video'


class Attachment(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    link = models.CharField(max_length=100)

    class Meta:
        db_table = u'attachment'


class Favorite(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    user = models.ForeignKey(User)
    date = models.DateTimeField(auto_now_add=True)


    class Meta:
        db_table = u'favorite'


class BrowseHistory(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    user = models.ForeignKey(User)
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = u'browse_history'


class Comment(models.Model):
    id = models.AutoField(primary_key=True)
    case = models.ForeignKey(Case)
    content = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User)

    class Meta:
        db_table = u'comment'


class Question(models.Model):
    id = models.AutoField(primary_key=True)
    content = models.TextField(blank=True)
    content_cn = models.TextField(blank=True)
    sort = models.IntegerField(blank=True)

    def __unicode__(self):
        return self.content_cn

    class Meta:
        db_table = u'question'


class Answer(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User)
    case=models.ForeignKey(Case)
    question = models.ForeignKey(Question)
    content = models.TextField()
    date = models.DateTimeField(auto_now_add=True)


    class Meta:
        db_table = u'answer'


class UserProfile(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(User)
    avatar = models.ImageField(upload_to='photos/%Y/%m/%d')
    activation_key = models.CharField(max_length=50, blank=True)
    gender = models.CharField(max_length=5, blank=True)
    age = models.CharField(max_length=5, blank=True)
    company = models.CharField(max_length=200)
    industry = models.CharField(max_length=50)
    scale = models.CharField(max_length=50,blank=True)
    position = models.CharField(max_length=50,blank=True)

    class Meta:
        db_table = u'user_profile'


class Contact(models.Model):
    id = models.AutoField(primary_key=True)
    email = models.EmailField(max_length=100, blank=True)
    name = models.CharField(max_length=100, blank=True)
    title = models.CharField(max_length=50, blank=True)
    file = models.FileField(upload_to='files/%Y/%m/%d', blank=True, null=True)
    content = models.CharField(max_length=500, blank=True)
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = u'contact'



class ContactForm(ModelForm):
    content = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Contact
        exclude = ('id', 'date')


class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile
        fields = ('gender', 'age','company','industry','scale','position')


class UserForm(ModelForm):
    password = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'size': 20, }))

    class Meta:
        model = User
        fields = ('username', 'email', 'password')


class LoginForm(ModelForm):
    username = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'size': 20, }))
    password = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'size': 20, }))

    class Meta:
        model = User
        fields = ('username', 'password')


class ResetPassForm(forms.Form):
    email = forms.EmailField(max_length='200')

    class Meta:
        pass



